<?php

include_once("../service/categoryService.php");
include_once("../service/dishService.php");
include_once("../util/characterUtils.php");

$catId = $_POST['cateid'];
$cateName = $_POST['catcontent'];
$hiddenCatName = $_POST['hiddencatcontent'];
$cateOrder = $_POST['catorder'];

if (isset($catId)) {
    $result = getAllCategory();
    $flag = true;
    while ($cat = ConnectionManager::getInstance()->fetchArray()) {
        if (removeSignOfString($cat["CAT_NAME"]) == removeSignOfString($cateName)) {
            $flag = false;
            break;
        }
    }
    if ($flag || $cateName == $hiddenCatName) {
        updateCategory($catId, $cateName, $cateOrder);
        Header("Location: ../admin-page/category.php?message_code=edited");
        return;
    } else {
        Header("Location: ../admin-page/category.php?message_code=unique");
        return;
    }
}
?>
